<template lang="jade">
div
  //- Loader
  h-loader-full(url='loader',:setdata='false')
  h-data-updater(url='cultivate/edit')
    m-title(icon='add',name='培训课程编辑')
    table.single
      mf-row(name='课程名称'): input(type = 'text',v-model='data.name')
      mf-row(name='开课时间'): input(type = 'datetime-local',v-model='data.class_starttime')
      mf-row(name='报名结束时间'): input(type = 'datetime-local',v-model='data.apply_endtime')
      mf-row(name='课程图片',v-if='data.type != 0'): mf-upload(uploadid='img',amount='1',rule='cultivate',:data='data.img',@upload_data='text')
      mf-row(name='跳转链接',v-if='data.type==1')
        input(type = 'text',v-model='data.url')
      mf-row(name='课程关键字',msg='长度不超过15个字！',v-if='data.type === 0')
        input(type = 'text',v-model='data.keyword')
      mf-row(name='课程类型') {{type[data.type]}}
      mf-row(name='上课地址',v-if='data.type === 0')
        input(type = 'text',v-model='data.address')
      mf-row(name='联系电话')
        input(type = 'text',v-model='data.mobile')
      mf-row(name='课程小图片',v-if='data.type === 0'): mf-upload(uploadid='smallimg',amount='1',rule='cultivate',:data='data.smallimg',@upload_data='text1')
      mf-row(name='课程内容',v-show='data.type == 1 || data.type == 2')
        textarea#editor.editor(style='width: 500px; height: 200px;', v-model='data.content') {{data.content}}
      mf-row(name='课程简介',v-show='data.type === 0')
        textarea#summary.editor(style='width: 500px; height: 200px;', v-model='data.summary') {{data.summary}}
      mf-row(name='课程简介',v-show='data.type === 0')
        textarea#merit.editor(style='width: 500px; height: 200px;', v-model='data.merit') {{data.merit}}
      mf-row
        button(id='button') 确认提交
</template>

<script>
export default {
	data () {
		return {
			loader:{
        detail:'cultivate/detail/' + this.$query.get('id') + '/' + this.$query.get('type')
      },
      type:['瑞安机电培训','职称考试','企业培训'],
      data:{},
      img:[]
		}
	},
	methods: {
    loadAfter (res) {
      this.data = res.detail[0][0]
      this.data.img = this.data.img.split('|')
      this.data.smallimg = this.data.smallimg.split('|')
      this.data.class_starttime = new Date(this.data.class_starttime * 1000).format("yyyy-MM-ddThh:mm")
      this.data.apply_endtime = new Date(this.data.apply_endtime * 1000).format("yyyy-MM-ddThh:mm")
      editor.sync();
      summary.sync();
      merit.sync();
      editor.html(this.data.content);
      summary.html(this.data.summary);
      merit.html(this.data.merit);
		},
    text(data){
			this.data.img = data
		},
    text1(data){
			this.data.smallimg = data
		},
    updateBefore(){
      editor.sync()
      summary.sync()
      merit.sync()
			this.data.content = this.$dom.id('editor').value
      this.data.summary = this.$dom.id('summary').value
      this.data.merit = this.$dom.id('merit').value
      if(this.data.name == ''){
        this.$msg.error('请填写课程名称!')
        return
      }
      if(this.data.class_starttime == ''){
        this.$msg.error('请选择开课时间!')
        return
      }
      if(this.data.apply_endtime == ''){
        this.$msg.error('请选择课程结束时间!')
        return
      }
      if(this.data.img.length == 0 && this.data.type != 0){
        this.$msg.error('请上传图片!')
        return
      }
      if(this.data.smallimg.length == 0 && this.data.type == 0){
        this.$msg.error('请上传小图片!')
        return
      }
      if(this.data.type != 0 && this.data.type != 1 && this.data.type != 2){
        this.$msg.error('请选择课程类型!')
        return
      }
      if(this.data.type == 0 && this.data.keyword == ''){
        this.$msg.error('请填写瑞安机电课程的课程关键字!')
        return
      }else if(this.data.keyword.length > 15){
        this.$msg.error('课程关键字的长度超出范围!')
        return
      }
      if(this.data.address == ''){
        this.$msg.error('请填写上课地址!')
        return
      }
      if(this.data.mobile == ''){
        this.$msg.error('请填写联系方式!')
        return
      }
      if(this.data.type != 0 && this.data.content == ''){
        this.$msg.error('请填写课程内容!')
        return
      }
      if(this.data.type == 0 && this.data.summary == ''){
        this.$msg.error('请填写瑞安机电课程的课程简介!')
        return
      }
      if(this.data.type == 0 && this.data.merit == ''){
        this.$msg.error('请填写瑞安机电课程的课程优势!')
        return
      }
      console.log(2222)
      return true
      
    },
    updateAfter (res) {
      console.log(3333)
			this.$msg.success()
			window.location.reload()
		},
	}
}
</script>
<style>
	input#img_small{display:none;}
</style>